﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonLibrary.Helpers;
using FileLibrary;
using FileLibrary.Enums;
using FileLibrary.Models;
using ShuntingScheduleUserControl.View;

namespace ShuntingScheduleUserControl
{
    public partial class ScheduleListForm : Form
    {
        private readonly Dictionary<int, ViewTrain> _viewShuntingScheduleByJobIds;
        private List<FileModel> fileModels;

        public ScheduleListForm(Dictionary<int, ViewTrain> viewShuntingScheduleByJobIds)
        {
            _viewShuntingScheduleByJobIds = viewShuntingScheduleByJobIds;
            InitializeComponent();
        }

        private void ScheduleListForm_Load(object sender, EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }


            fileModels = FileManage.Instance.FileModes.Values.ToList();
            dataGridView1.DataSource = _viewShuntingScheduleByJobIds.Values.SelectMany(x => x.ViewSchedulesByJobId).Select(y => y.Schedule).ToList();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value == null) return;
            var clm = this.dataGridView1.Columns[e.ColumnIndex];
            if (clm.DataPropertyName == "StartTime" || clm.DataPropertyName == "EndTime")
            {
                //e.Value = int.Parse((string)e.Value).GetDateTimeByMinute();
                var eValue = (int)e.Value;
                e.Value = eValue.GetDateTimeByMinute().ToString($"HH:mm");
                e.FormattingApplied = true;
            }
            else if (clm.DataPropertyName == "MachId")
            {
                var name = fileModels.First(x => x is Track && x.Index == (int)e.Value);
                e.Value = ((Track)name).Name;
                e.FormattingApplied = true;
            }
            else if(clm.DataPropertyName == "JobId")
            {
                //var name = fileModels.First(x => x is TrainPlan && x.Index == (int)e.Value);
                ////e.Value = ((TrainPlan)name).TrainId;
                //e.FormattingApplied = true;
            }
            else if (clm.DataPropertyName == "ProcId")
            {
                //var type = (MaintenanceEnum) e.Value;
                //e.FormattingApplied = true;
            }
        }
    }
}
